<template>
  <el-dialog
    :title="'审核'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      
        <el-form-item label="用户名" prop="userName" >
          <el-input v-model="dataForm.userName" disabled placeholder="登录帐号"></el-input>
        </el-form-item> 

        <el-form-item label="用户状态" prop="checkStatus">
          <el-select v-model="dataForm.checkStatus" clearable placeholder="请选择">
            <el-option label="不通过" value="-1"></el-option>
            <el-option label="通过" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不通过原因" prop="uncheckReason">
        <el-input type="textarea" v-model="dataForm.uncheckReason"></el-input>
      </el-form-item>
      
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      
      return {
        visible: false,
        dataForm: {
          id: 0,
          userName: '',
          //tenantId: '1',
          password: '',
          comfirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          roleIdList: [],
          //status: 1
          province: '',
          city: '',
          country: '',
          employer: '',
          position: '',
          checkStatus: '',
          uncheckReason: ''
        },
        dataRule: {
          
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        
        this.$http({
          url: this.$http.adornUrl('/sys/role/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.username;
                //this.dataForm.tenantId = data.user.tenantId
                this.dataForm.salt = data.user.salt;
                this.dataForm.email = data.user.email;
                this.dataForm.mobile = data.user.mobile;
                this.dataForm.roleIdList = data.user.roleIdList;
                //this.dataForm.status = data.user.status
                this.dataForm.province = data.user.province;
                
                this.dataForm.city = data.user.city;
                this.dataForm.country = data.user.country;
                if(this.dataForm.province){
                  this.loadCity(this.dataForm.province);
                }
                if(this.dataForm.city){
                  this.loadCountry(this.dataForm.city);
                }
                this.dataForm.employer = data.user.employer;
                this.dataForm.position = data.user.position;

                //this.dataForm.checkStatus = data.user.checkStatus;
                //this.dataForm.uncheckReason = data.user.uncheckReason;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.id || undefined,
                'username': this.dataForm.userName,
                //'tenantId': this.dataForm.tenantId,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                //'status': this.dataForm.status,
                'roleIdList': this.dataForm.roleIdList,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'country': this.dataForm.country,
                'employer': this.dataForm.employer,
                'position': this.dataForm.position,
                'checkStatus': this.dataForm.checkStatus,
                'uncheckReason': this.dataForm.uncheckReason,
                'flag': 'check'
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      //加载市级行政区
      loadCity(curVal){
        this.cityList = [];
        this.$http({
            url: this.$http.adornUrl('/frt/district/select'),
            method: 'get',
            params: this.$http.adornParams({
              'level': 2,
              'parentCode': curVal
            })
          }).then(({data}) => {
            this.cityList = data.list;
          })
      },
      //加载县级行政区
      loadCountry(curVal){
        this.countryList = [];
        this.$http({
            url: this.$http.adornUrl('/frt/district/select'),
            method: 'get',
            params: this.$http.adornParams({
              'level': 3,
              'parentCode': curVal
            })
          }).then(({data}) => {
            this.countryList = data.list;
          })
      }
    }
  }
</script>
